

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.trial._trial &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.trial._trial</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.trial._trial 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">CategoricalDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">DiscreteUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">IntLogUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">IntUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">LogUniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">UniformDistribution</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">logging</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">pruners</span>
<span class="kn">from</span> <span class="nn">optuna.trial._base</span> <span class="kn">import</span> <span class="n">BaseTrial</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">datetime</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Sequence</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>  <span class="c1"># NOQA</span>

    <span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">BaseDistribution</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">CategoricalChoiceType</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>  <span class="c1"># NOQA</span>

    <span class="n">FloatingPointDistributionType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">UniformDistribution</span><span class="p">,</span> <span class="n">LogUniformDistribution</span><span class="p">]</span>


<span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="Trial"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial">[文档]</a><span class="k">class</span> <span class="nc">Trial</span><span class="p">(</span><span class="n">BaseTrial</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A trial is a process of evaluating an objective function.</span>

<span class="sd">    This object is passed to an objective function and provides interfaces to get parameter</span>
<span class="sd">    suggestion, manage the trial&#39;s state, and set/get user-defined attributes of the trial.</span>

<span class="sd">    Note that the direct use of this constructor is not recommended.</span>
<span class="sd">    This object is seamlessly instantiated and passed to the objective function behind</span>
<span class="sd">    the :func:`optuna.study.Study.optimize()` method; hence library users do not care about</span>
<span class="sd">    instantiation of this object.</span>

<span class="sd">    Args:</span>
<span class="sd">        study:</span>
<span class="sd">            A :class:`~optuna.study.Study` object.</span>
<span class="sd">        trial_id:</span>
<span class="sd">            A trial ID that is automatically generated.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">study</span><span class="p">,</span>  <span class="c1"># type: Study</span>
        <span class="n">trial_id</span><span class="p">,</span>  <span class="c1"># type: int</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="o">=</span> <span class="n">study</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span> <span class="o">=</span> <span class="n">trial_id</span>

        <span class="c1"># TODO(Yanase): Remove _study_id attribute, and use study._study_id instead.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_study_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">_study_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">_storage</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_init_relative_params</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_init_relative_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span>

        <span class="n">study</span> <span class="o">=</span> <span class="n">pruners</span><span class="o">.</span><span class="n">_filter_study</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">relative_search_space</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">sampler</span><span class="o">.</span><span class="n">infer_relative_search_space</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">relative_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">sampler</span><span class="o">.</span><span class="n">sample_relative</span><span class="p">(</span>
            <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_search_space</span>
        <span class="p">)</span>

<div class="viewcode-block" id="Trial.suggest_float"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_float">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_float</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">low</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="n">high</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
        <span class="o">*</span><span class="p">,</span>
        <span class="n">step</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">log</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the floating point parameter.</span>

<span class="sd">        Note that this is a wrapper method for :func:`~optuna.trial.Trial.suggest_uniform`,</span>
<span class="sd">        :func:`~optuna.trial.Trial.suggest_loguniform` and</span>
<span class="sd">        :func:`~optuna.trial.Trial.suggest_discrete_uniform`.</span>

<span class="sd">        .. versionadded:: 1.3.0</span>

<span class="sd">        .. seealso::</span>
<span class="sd">            Please see also :func:`~optuna.trial.Trial.suggest_uniform`,</span>
<span class="sd">            :func:`~optuna.trial.Trial.suggest_loguniform` and</span>
<span class="sd">            :func:`~optuna.trial.Trial.suggest_discrete_uniform`.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest a momentum, learning rate and scaling factor of learning rate</span>
<span class="sd">            for neural network training.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>
<span class="sd">                from sklearn.neural_network import MLPClassifier</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=0)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    momentum = trial.suggest_float(&#39;momentum&#39;, 0.0, 1.0)</span>
<span class="sd">                    learning_rate_init = trial.suggest_float(&#39;learning_rate_init&#39;,</span>
<span class="sd">                                                             1e-5, 1e-3, log=True)</span>
<span class="sd">                    power_t = trial.suggest_float(&#39;power_t&#39;, 0.2, 0.8, step=0.1)</span>
<span class="sd">                    clf = MLPClassifier(hidden_layer_sizes=(100, 50), momentum=momentum,</span>
<span class="sd">                                        learning_rate_init=learning_rate_init,</span>
<span class="sd">                                        solver=&#39;sgd&#39;, random_state=0, power_t=power_t)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>

<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>

<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            low:</span>
<span class="sd">                Lower endpoint of the range of suggested values. ``low`` is included in the range.</span>
<span class="sd">            high:</span>
<span class="sd">                Upper endpoint of the range of suggested values. ``high`` is excluded from the</span>
<span class="sd">                range.</span>
<span class="sd">            step:</span>
<span class="sd">                A step of discretization.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    The ``step`` and ``log`` arguments cannot be used at the same time. To set</span>
<span class="sd">                    the ``step`` argument to a float number, set the ``log`` argument to ``False``.</span>
<span class="sd">            log:</span>
<span class="sd">                A flag to sample the value from the log domain or not.</span>
<span class="sd">                If ``log`` is true, the value is sampled from the range in the log domain.</span>
<span class="sd">                Otherwise, the value is sampled from the range in the linear domain.</span>
<span class="sd">                See also :func:`suggest_uniform` and :func:`suggest_loguniform`.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    The ``step`` and ``log`` arguments cannot be used at the same time. To set</span>
<span class="sd">                    the ``log`` argument to ``True``, set the ``step`` argument to ``None``.</span>

<span class="sd">        Raises:</span>
<span class="sd">            :exc:`ValueError`:</span>
<span class="sd">                If ``step is not None`` and ``log = True`` are specified.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A suggested float value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">log</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The parameter `step` is not supported when `log` is True.&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">suggest_discrete_uniform</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">log</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">suggest_loguniform</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.suggest_uniform"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_uniform">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_uniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">):</span>
        <span class="c1"># type: (str, float, float) -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the continuous parameter.</span>

<span class="sd">        The value is sampled from the range :math:`[\\mathsf{low}, \\mathsf{high})`</span>
<span class="sd">        in the linear domain. When :math:`\\mathsf{low} = \\mathsf{high}`, the value of</span>
<span class="sd">        :math:`\\mathsf{low}` will be returned.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest a momentum for neural network training.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>
<span class="sd">                from sklearn.neural_network import MLPClassifier</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    momentum = trial.suggest_uniform(&#39;momentum&#39;, 0.0, 1.0)</span>
<span class="sd">                    clf = MLPClassifier(hidden_layer_sizes=(100, 50), momentum=momentum,</span>
<span class="sd">                                        solver=&#39;sgd&#39;, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>

<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>

<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            low:</span>
<span class="sd">                Lower endpoint of the range of suggested values. ``low`` is included in the range.</span>
<span class="sd">            high:</span>
<span class="sd">                Upper endpoint of the range of suggested values. ``high`` is excluded from the</span>
<span class="sd">                range.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A suggested float value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">distribution</span> <span class="o">=</span> <span class="n">UniformDistribution</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_distribution</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.suggest_loguniform"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_loguniform">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_loguniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">):</span>
        <span class="c1"># type: (str, float, float) -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the continuous parameter.</span>

<span class="sd">        The value is sampled from the range :math:`[\\mathsf{low}, \\mathsf{high})`</span>
<span class="sd">        in the log domain. When :math:`\\mathsf{low} = \\mathsf{high}`, the value of</span>
<span class="sd">        :math:`\\mathsf{low}` will be returned.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest penalty parameter ``C`` of `SVC &lt;https://scikit-learn.org/stable/modules/</span>
<span class="sd">            generated/sklearn.svm.SVC.html&gt;`_.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>
<span class="sd">                from sklearn.svm import SVC</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    c = trial.suggest_loguniform(&#39;c&#39;, 1e-5, 1e2)</span>
<span class="sd">                    clf = SVC(C=c, gamma=&#39;scale&#39;, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>
<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>

<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            low:</span>
<span class="sd">                Lower endpoint of the range of suggested values. ``low`` is included in the range.</span>
<span class="sd">            high:</span>
<span class="sd">                Upper endpoint of the range of suggested values. ``high`` is excluded from the</span>
<span class="sd">                range.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A suggested float value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">distribution</span> <span class="o">=</span> <span class="n">LogUniformDistribution</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_distribution</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.suggest_discrete_uniform"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_discrete_uniform">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_discrete_uniform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="p">):</span>
        <span class="c1"># type: (str, float, float, float) -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the discrete parameter.</span>

<span class="sd">        The value is sampled from the range :math:`[\\mathsf{low}, \\mathsf{high}]`,</span>
<span class="sd">        and the step of discretization is :math:`q`. More specifically,</span>
<span class="sd">        this method returns one of the values in the sequence</span>
<span class="sd">        :math:`\\mathsf{low}, \\mathsf{low} + q, \\mathsf{low} + 2 q, \\dots,</span>
<span class="sd">        \\mathsf{low} + k q \\le \\mathsf{high}`,</span>
<span class="sd">        where :math:`k` denotes an integer. Note that :math:`high` may be changed due to round-off</span>
<span class="sd">        errors if :math:`q` is not an integer. Please check warning messages to find the changed</span>
<span class="sd">        values.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest a fraction of samples used for fitting the individual learners of</span>
<span class="sd">            `GradientBoostingClassifier &lt;https://scikit-learn.org/stable/modules/generated/</span>
<span class="sd">            sklearn.ensemble.GradientBoostingClassifier.html&gt;`_.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.ensemble import GradientBoostingClassifier</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    subsample = trial.suggest_discrete_uniform(&#39;subsample&#39;, 0.1, 1.0, 0.1)</span>
<span class="sd">                    clf = GradientBoostingClassifier(subsample=subsample, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>
<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>

<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            low:</span>
<span class="sd">                Lower endpoint of the range of suggested values. ``low`` is included in the range.</span>
<span class="sd">            high:</span>
<span class="sd">                Upper endpoint of the range of suggested values. ``high`` is included in the range.</span>
<span class="sd">            q:</span>
<span class="sd">                A step of discretization.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A suggested float value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">distribution</span> <span class="o">=</span> <span class="n">DiscreteUniformDistribution</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">q</span><span class="o">=</span><span class="n">q</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_distribution</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.suggest_int"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_int">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_int</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">low</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">step</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">log</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the integer parameter.</span>

<span class="sd">        The value is sampled from the integers in :math:`[\\mathsf{low}, \\mathsf{high}]`.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest the number of trees in `RandomForestClassifier &lt;https://scikit-learn.org/</span>
<span class="sd">            stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html&gt;`_.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.ensemble import RandomForestClassifier</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    n_estimators = trial.suggest_int(&#39;n_estimators&#39;, 50, 400)</span>
<span class="sd">                    clf = RandomForestClassifier(n_estimators=n_estimators, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>
<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>

<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            low:</span>
<span class="sd">                Lower endpoint of the range of suggested values. ``low`` is included in the range.</span>
<span class="sd">            high:</span>
<span class="sd">                Upper endpoint of the range of suggested values. ``high`` is included in the range.</span>
<span class="sd">            step:</span>
<span class="sd">                A step of discretization.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    Note that :math:`\\mathsf{high}` is modified if the range is not divisible by</span>
<span class="sd">                    :math:`\\mathsf{step}`. Please check the warning messages to find the changed</span>
<span class="sd">                    values.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    The method returns one of the values in the sequence</span>
<span class="sd">                    :math:`\\mathsf{low}, \\mathsf{low} + \\mathsf{step}, \\mathsf{low} + 2 *</span>
<span class="sd">                    \\mathsf{step}, \\dots, \\mathsf{low} + k * \\mathsf{step} \\le</span>
<span class="sd">                    \\mathsf{high}`, where :math:`k` denotes an integer.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    The ``step != 1`` and ``log`` arguments cannot be used at the same time.</span>
<span class="sd">                    To set the ``step`` argument :math:`\\mathsf{step} \\ge 2`, set the</span>
<span class="sd">                    ``log`` argument to ``False``.</span>
<span class="sd">            log:</span>
<span class="sd">                A flag to sample the value from the log domain or not.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    If ``log`` is true, at first, the range of suggested values is divided into</span>
<span class="sd">                    grid points of width 1. The range of suggested values is then converted to</span>
<span class="sd">                    a log domain, from which a value is sampled. The uniformly sampled</span>
<span class="sd">                    value is re-converted to the original domain and rounded to the nearest grid</span>
<span class="sd">                    point that we just split, and the suggested value is determined.</span>
<span class="sd">                    For example, if `low = 2` and `high = 8`, then the range of suggested values is</span>
<span class="sd">                    `[2, 3, 4, 5, 6, 7, 8]` and lower values tend to be more sampled than higher</span>
<span class="sd">                    values.</span>

<span class="sd">                .. note::</span>
<span class="sd">                    The ``step != 1`` and ``log`` arguments cannot be used at the same time.</span>
<span class="sd">                    To set the ``log`` argument to ``True``, set the ``step`` argument to 1.</span>

<span class="sd">        Raises:</span>
<span class="sd">            :exc:`ValueError`:</span>
<span class="sd">                If ``step != 1`` and ``log = True`` are specified.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">step</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">log</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;The parameter `step != 1` is not supported when `log` is True.&quot;</span>
                    <span class="s2">&quot;The specified `step` is </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">distribution</span> <span class="o">=</span> <span class="n">IntUniformDistribution</span><span class="p">(</span>
                    <span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span>
                <span class="p">)</span>  <span class="c1"># type: Union[IntUniformDistribution, IntLogUniformDistribution]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">log</span><span class="p">:</span>
                <span class="n">distribution</span> <span class="o">=</span> <span class="n">IntLogUniformDistribution</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">distribution</span> <span class="o">=</span> <span class="n">IntUniformDistribution</span><span class="p">(</span><span class="n">low</span><span class="o">=</span><span class="n">low</span><span class="p">,</span> <span class="n">high</span><span class="o">=</span><span class="n">high</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_distribution</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">))</span></div>

<div class="viewcode-block" id="Trial.suggest_categorical"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.suggest_categorical">[文档]</a>    <span class="k">def</span> <span class="nf">suggest_categorical</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">choices</span><span class="p">):</span>
        <span class="c1"># type: (str, Sequence[CategoricalChoiceType]) -&gt; CategoricalChoiceType</span>
        <span class="sd">&quot;&quot;&quot;Suggest a value for the categorical parameter.</span>

<span class="sd">        The value is sampled from ``choices``.</span>

<span class="sd">        Example:</span>

<span class="sd">            Suggest a kernel function of `SVC &lt;https://scikit-learn.org/stable/modules/generated/</span>
<span class="sd">            sklearn.svm.SVC.html&gt;`_.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>
<span class="sd">                from sklearn.svm import SVC</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    kernel = trial.suggest_categorical(&#39;kernel&#39;, [&#39;linear&#39;, &#39;poly&#39;, &#39;rbf&#39;])</span>
<span class="sd">                    clf = SVC(kernel=kernel, gamma=&#39;scale&#39;, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>
<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>


<span class="sd">        Args:</span>
<span class="sd">            name:</span>
<span class="sd">                A parameter name.</span>
<span class="sd">            choices:</span>
<span class="sd">                Parameter value candidates.</span>

<span class="sd">        .. seealso::</span>
<span class="sd">            :class:`~optuna.distributions.CategoricalDistribution`.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A suggested value.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">choices</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">choices</span><span class="p">)</span>

        <span class="c1"># There is no need to call self._check_distribution because</span>
        <span class="c1"># CategoricalDistribution does not support dynamic value space.</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">CategoricalDistribution</span><span class="p">(</span><span class="n">choices</span><span class="o">=</span><span class="n">choices</span><span class="p">))</span></div>

<div class="viewcode-block" id="Trial.report"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.report">[文档]</a>    <span class="k">def</span> <span class="nf">report</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="c1"># type: (float, int) -&gt; None</span>
        <span class="sd">&quot;&quot;&quot;Report an objective function value for a given step.</span>

<span class="sd">        The reported values are used by the pruners to determine whether this trial should be</span>
<span class="sd">        pruned.</span>

<span class="sd">        .. seealso::</span>
<span class="sd">            Please refer to :class:`~optuna.pruners.BasePruner`.</span>

<span class="sd">        .. note::</span>
<span class="sd">            The reported value is converted to ``float`` type by applying ``float()``</span>
<span class="sd">            function internally. Thus, it accepts all float-like types (e.g., ``numpy.float32``).</span>
<span class="sd">            If the conversion fails, a ``TypeError`` is raised.</span>

<span class="sd">        Example:</span>

<span class="sd">            Report intermediate scores of `SGDClassifier &lt;https://scikit-learn.org/stable/modules/</span>
<span class="sd">            generated/sklearn.linear_model.SGDClassifier.html&gt;`_ training.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.linear_model import SGDClassifier</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    clf = SGDClassifier(random_state=0)</span>
<span class="sd">                    for step in range(100):</span>
<span class="sd">                        clf.partial_fit(X_train, y_train, np.unique(y))</span>
<span class="sd">                        intermediate_value = clf.score(X_valid, y_valid)</span>
<span class="sd">                        trial.report(intermediate_value, step=step)</span>
<span class="sd">                        if trial.should_prune():</span>
<span class="sd">                            raise optuna.TrialPruned()</span>

<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>


<span class="sd">        Args:</span>
<span class="sd">            value:</span>
<span class="sd">                A value returned from the objective function.</span>
<span class="sd">            step:</span>
<span class="sd">                Step of the trial (e.g., Epoch of neural network training).</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># For convenience, we allow users to report a value that can be cast to `float`.</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
            <span class="n">message</span> <span class="o">=</span> <span class="s2">&quot;The `value` argument is of type &#39;</span><span class="si">{}</span><span class="s2">&#39; but supposed to be a float.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="p">)</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">step</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The `step` argument is </span><span class="si">{}</span><span class="s2"> but cannot be negative.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step</span><span class="p">))</span>

        <span class="n">intermediate_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">intermediate_values</span>

        <span class="k">if</span> <span class="n">step</span> <span class="ow">in</span> <span class="n">intermediate_values</span><span class="p">:</span>
            <span class="c1"># Do nothing if already reported.</span>
            <span class="c1"># TODO(hvy): Consider raising a warning or an error.</span>
            <span class="c1"># See https://github.com/optuna/optuna/issues/852.</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">set_trial_intermediate_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.should_prune"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.should_prune">[文档]</a>    <span class="k">def</span> <span class="nf">should_prune</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Suggest whether the trial should be pruned or not.</span>

<span class="sd">        The suggestion is made by a pruning algorithm associated with the trial and is based on</span>
<span class="sd">        previously reported values. The algorithm can be specified when constructing a</span>
<span class="sd">        :class:`~optuna.study.Study`.</span>

<span class="sd">        .. note::</span>
<span class="sd">            If no values have been reported, the algorithm cannot make meaningful suggestions.</span>
<span class="sd">            Similarly, if this method is called multiple times with the exact same set of reported</span>
<span class="sd">            values, the suggestions will be the same.</span>

<span class="sd">        .. seealso::</span>
<span class="sd">            Please refer to the example code in :func:`optuna.trial.Trial.report`.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A boolean value. If :obj:`True`, the trial should be pruned according to the</span>
<span class="sd">            configured pruning algorithm. Otherwise, the trial should continue.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">_storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">pruner</span><span class="o">.</span><span class="n">prune</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span></div>

<div class="viewcode-block" id="Trial.set_user_attr"><a class="viewcode-back" href="../../../reference/trial.html#optuna.trial.Trial.set_user_attr">[文档]</a>    <span class="k">def</span> <span class="nf">set_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (str, Any) -&gt; None</span>
        <span class="sd">&quot;&quot;&quot;Set user attributes to the trial.</span>

<span class="sd">        The user attributes in the trial can be access via :func:`optuna.trial.Trial.user_attrs`.</span>

<span class="sd">        Example:</span>

<span class="sd">            Save fixed hyperparameters of neural network training.</span>

<span class="sd">            .. testcode::</span>

<span class="sd">                import numpy as np</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.model_selection import train_test_split</span>
<span class="sd">                from sklearn.neural_network import MLPClassifier</span>

<span class="sd">                import optuna</span>

<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=0)</span>

<span class="sd">                def objective(trial):</span>
<span class="sd">                    trial.set_user_attr(&#39;BATCHSIZE&#39;, 128)</span>
<span class="sd">                    momentum = trial.suggest_uniform(&#39;momentum&#39;, 0, 1.0)</span>
<span class="sd">                    clf = MLPClassifier(hidden_layer_sizes=(100, 50),</span>
<span class="sd">                                        batch_size=trial.user_attrs[&#39;BATCHSIZE&#39;],</span>
<span class="sd">                                        momentum=momentum, solver=&#39;sgd&#39;, random_state=0)</span>
<span class="sd">                    clf.fit(X_train, y_train)</span>

<span class="sd">                    return clf.score(X_valid, y_valid)</span>

<span class="sd">                study = optuna.create_study(direction=&#39;maximize&#39;)</span>
<span class="sd">                study.optimize(objective, n_trials=3)</span>
<span class="sd">                assert &#39;BATCHSIZE&#39; in study.best_trial.user_attrs.keys()</span>
<span class="sd">                assert study.best_trial.user_attrs[&#39;BATCHSIZE&#39;] == 128</span>


<span class="sd">        Args:</span>
<span class="sd">            key:</span>
<span class="sd">                A key string of the attribute.</span>
<span class="sd">            value:</span>
<span class="sd">                A value of the attribute. The value should be JSON serializable.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">set_trial_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">set_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="c1"># type: (str, Any) -&gt; None</span>
        <span class="sd">&quot;&quot;&quot;Set system attributes to the trial.</span>

<span class="sd">        Note that Optuna internally uses this method to save system messages such as failure</span>
<span class="sd">        reason of trials. Please use :func:`~optuna.trial.Trial.set_user_attr` to set users&#39;</span>
<span class="sd">        attributes.</span>

<span class="sd">        Args:</span>
<span class="sd">            key:</span>
<span class="sd">                A key string of the attribute.</span>
<span class="sd">            value:</span>
<span class="sd">                A value of the attribute. The value should be JSON serializable.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">set_trial_system_attr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_suggest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (str, BaseDistribution) -&gt; Any</span>

        <span class="n">storage</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span>
        <span class="n">trial_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span>

        <span class="n">trial</span> <span class="o">=</span> <span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">:</span>
            <span class="c1"># No need to sample if already suggested.</span>
            <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span><span class="n">trial</span><span class="o">.</span><span class="n">distributions</span><span class="p">[</span><span class="n">name</span><span class="p">],</span> <span class="n">distribution</span><span class="p">)</span>
            <span class="n">param_value</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_external_repr</span><span class="p">(</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_param</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_fixed_param</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
                <span class="n">param_value</span> <span class="o">=</span> <span class="n">storage</span><span class="o">.</span><span class="n">get_trial_system_attrs</span><span class="p">(</span><span class="n">trial_id</span><span class="p">)[</span><span class="s2">&quot;fixed_params&quot;</span><span class="p">][</span><span class="n">name</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">distribution</span><span class="o">.</span><span class="n">single</span><span class="p">():</span>
                <span class="n">param_value</span> <span class="o">=</span> <span class="n">distributions</span><span class="o">.</span><span class="n">_get_single_value</span><span class="p">(</span><span class="n">distribution</span><span class="p">)</span>
            <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_relative_param</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
                <span class="n">param_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_params</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">study</span> <span class="o">=</span> <span class="n">pruners</span><span class="o">.</span><span class="n">_filter_study</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">)</span>
                <span class="n">param_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">sampler</span><span class="o">.</span><span class="n">sample_independent</span><span class="p">(</span>
                    <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span>
                <span class="p">)</span>

            <span class="n">param_value_in_internal_repr</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>
            <span class="n">storage</span><span class="o">.</span><span class="n">set_trial_param</span><span class="p">(</span><span class="n">trial_id</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">param_value_in_internal_repr</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">param_value</span>

    <span class="k">def</span> <span class="nf">_is_fixed_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (str, BaseDistribution) -&gt; bool</span>

        <span class="n">system_attrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="s2">&quot;fixed_params&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">system_attrs</span><span class="p">[</span><span class="s2">&quot;fixed_params&quot;</span><span class="p">]:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">param_value</span> <span class="o">=</span> <span class="n">system_attrs</span><span class="p">[</span><span class="s2">&quot;fixed_params&quot;</span><span class="p">][</span><span class="n">name</span><span class="p">]</span>
        <span class="n">param_value_in_internal_repr</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>

        <span class="n">contained</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">_contains</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">contained</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s2">&quot;Fixed parameter &#39;</span><span class="si">{}</span><span class="s2">&#39; with value </span><span class="si">{}</span><span class="s2"> is out of range &quot;</span>
                <span class="s2">&quot;for distribution </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">param_value</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">contained</span>

    <span class="k">def</span> <span class="nf">_is_relative_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (str, BaseDistribution) -&gt; bool</span>

        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_params</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_search_space</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;The parameter &#39;</span><span class="si">{}</span><span class="s2">&#39; was sampled by `sample_relative` method &quot;</span>
                <span class="s2">&quot;but it is not contained in the relative search space.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="n">relative_distribution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_search_space</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="n">distributions</span><span class="o">.</span><span class="n">check_distribution_compatibility</span><span class="p">(</span><span class="n">relative_distribution</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>

        <span class="n">param_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">relative_params</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="n">param_value_in_internal_repr</span> <span class="o">=</span> <span class="n">distribution</span><span class="o">.</span><span class="n">to_internal_repr</span><span class="p">(</span><span class="n">param_value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">distribution</span><span class="o">.</span><span class="n">_contains</span><span class="p">(</span><span class="n">param_value_in_internal_repr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_distribution</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">):</span>
        <span class="c1"># type: (str, BaseDistribution) -&gt; None</span>

        <span class="n">old_distribution</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">distributions</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
            <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="n">old_distribution</span> <span class="o">!=</span> <span class="n">distribution</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;Inconsistent parameter values for distribution with name &quot;</span><span class="si">{}</span><span class="s1">&quot;! &#39;</span>
                <span class="s2">&quot;This might be a configuration mistake. &quot;</span>
                <span class="s2">&quot;Optuna allows to call the same distribution with the same &quot;</span>
                <span class="s2">&quot;name more then once in a trial. &quot;</span>
                <span class="s2">&quot;When the parameter values are inconsistent optuna only &quot;</span>
                <span class="s2">&quot;uses the values of the first call and ignores all following. &quot;</span>
                <span class="s2">&quot;Using these values: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">old_distribution</span><span class="o">.</span><span class="n">_asdict</span><span class="p">()),</span>
                <span class="ne">RuntimeWarning</span><span class="p">,</span>
            <span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">number</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; int</span>
        <span class="sd">&quot;&quot;&quot;Return trial&#39;s number which is consecutive and unique in a study.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A trial number.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_number_from_id</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Return parameters to be optimized.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all parameters.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_params</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">distributions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, BaseDistribution]</span>
        <span class="sd">&quot;&quot;&quot;Return distributions of parameters to be optimized.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all distributions.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">distributions</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Return user attributes.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all user attributes.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_user_attrs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Return system attributes.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing all system attributes.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial_system_attrs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">datetime_start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Optional[datetime.datetime]</span>
        <span class="sd">&quot;&quot;&quot;Return start datetime.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Datetime where the :class:`~optuna.trial.Trial` started.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_trial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_trial_id</span><span class="p">)</span><span class="o">.</span><span class="n">datetime_start</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>